using System;
using System.IO;
using System.Xml;
using System.Collections;

namespace sforceExplorer
{
	/// <summary>
	/// Summary description for dbRegConfig.
	/// </summary>
	public class dbRepConfigBean
	{

		#region properties
		private decimal _interval = 5;
		private bool _updates = true;
		private bool _deletes = false;
		private ArrayList _entities = new ArrayList();
		private bool loaded = false;
		private int _startTimeOffset = 0;
		private ApiBrowserControl.SelectListChangedArguments[] retrieveObjects;

		public decimal interval
		{
			get 
			{
				if (!loaded) Load();
				return _interval; 
			}
			set { _interval = value; }
		}
		public bool updates
		{
			get 
			{
				if (!loaded) Load();
				return _updates; 
			}
			set { _updates = value; }
		}
		public bool deletes 
		{
			get 
			{
				if (!loaded) Load();
				return _deletes; 
			}
			set {_deletes = value; }
		}
		public ArrayList entities
		{
			get 
			{
				if (!loaded) Load();
				return _entities; 
			}
			set {_entities = value; }
		}
		public int StartTimeOffset
		{
			get { return _startTimeOffset; }
			set { _startTimeOffset = value; }
		}
		public ApiBrowserControl.SelectListChangedArguments[] RetrieveObjects
		{
			get { return retrieveObjects; }
			set { retrieveObjects = value; }
		}
		#endregion

		public void Load() 
		{
			DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory);
			FileSystemInfo[] fi = di.GetFiles("*.xml");
			XmlDocument doc = null;
			if (fi != null) 
			{
				for (int i =0;i<fi.Length;i++) 
				{
					if (fi[0].Name.ToLower().Equals("dbrep.xml"))
					{
						doc = new XmlDocument();
						doc.Load(fi[0].FullName);
						break;
					}
				}
				
				if (doc == null) 
				{
					//Default polling options and empty selection set
					this.interval = 5;
					this.deletes = true;
					this.updates = true;
				} 
				else 
				{
					XmlNode child = doc.FirstChild;
					if (child != null) 
					{
						XmlNode xn = doc.SelectSingleNode(@"/settings/interval");
						this.interval = Convert.ToDecimal(xn.InnerText);
						xn = doc.SelectSingleNode(@"/settings/updates");
						this.updates = xn.InnerText.Equals("true");
						xn = doc.SelectSingleNode(@"/settings/starttimeoffset");
						if (xn != null)
							this.StartTimeOffset = Convert.ToInt16(xn.InnerText);
						xn = doc.SelectSingleNode(@"/settings/deletes");
						this.deletes = xn.InnerText.Equals("true");
						xn = doc.SelectSingleNode(@"/settings/objects");
						if (xn.ChildNodes.Count > 0) 
						{
							for (int index=0;index<xn.ChildNodes.Count;index++) 
							{
								_entities.Add(xn.ChildNodes[index].InnerText);
							}
						}
						xn = doc.SelectSingleNode(@"/settings/retrieveObjects");
						if (xn != null) 
						{
							ArrayList ro = new ArrayList();
							for (int index=0;index<xn.ChildNodes.Count;index++) 
							{
								System.Xml.XmlNode objectNode = xn.ChildNodes[index];
								ApiBrowserControl.SelectListChangedArguments slca = new sforceExplorer.ApiBrowserControl.SelectListChangedArguments();
								for (int j=0;j<objectNode.ChildNodes.Count;j++) 
								{
									System.Xml.XmlNode objectChildNode = objectNode.ChildNodes[j];
									if (objectChildNode.LocalName == "fields") 
									{
										System.Xml.XmlNodeList xnl = objectChildNode.ChildNodes;
										for (int i=0;i<xnl.Count;i++) 
										{
											slca.AddField(xnl[i].InnerText);
										}
									} 
									else 
									{
										slca.TableName = objectChildNode.InnerText;
									}
								}
								ro.Add(slca);
							}
							this.retrieveObjects = (ApiBrowserControl.SelectListChangedArguments[])ro.ToArray(typeof(ApiBrowserControl.SelectListChangedArguments));
						}
					}
				}
			}
			loaded = true;
		}

		public void Save() 
		{
			System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Environment.CurrentDirectory);
			System.IO.FileSystemInfo[] fi = di.GetFiles("*.xml");
			System.Xml.XmlTextWriter xw = null;
			if (fi != null) 
			{
				for (int i =0;i<fi.Length;i++) 
				{
					if (fi[0].Name.ToLower().Equals("dbrep.xml"))
					{
						File.Delete(fi[0].FullName);
						break;
					}
				}
			}
			xw = new XmlTextWriter(di.FullName + @"\dbrep.xml", System.Text.Encoding.UTF8);
			xw.Formatting = Formatting.Indented;

			xw.WriteStartDocument(true);
			//Write the ProcessingInstruction node.
			xw.Flush();

			xw.WriteDocType("sforce", null, null, null);
			xw.WriteStartElement("settings");
			xw.WriteElementString("interval", this.interval.ToString());
			xw.Flush();

			if (this.retrieveObjects != null) 
			{
				xw.WriteStartElement("retrieveObjects");
				for (int i=0;i<this.retrieveObjects.Length;i++) 
				{
					xw.WriteStartElement("object");
					xw.WriteElementString("tableName", this.retrieveObjects[i].TableName);
					xw.WriteStartElement("fields");
					for (int j=0;j<this.retrieveObjects[i].SelectedFields.Length;j++) 
					{
						xw.WriteElementString("field", this.retrieveObjects[i].SelectedFields[j]);
					}
					xw.WriteEndElement();
					xw.WriteEndElement();
				}
				xw.WriteEndElement();
				xw.Flush();
			}

			if (this.updates)
				xw.WriteElementString("updates", "true");
			else
				xw.WriteElementString("updates", "false");

			if (this.deletes)
				xw.WriteElementString("deletes", "true");
			else
				xw.WriteElementString("deletes", "false");

			xw.WriteElementString("starttimeoffset", this._startTimeOffset.ToString());

			xw.WriteStartElement("objects");
			for (int i=0;i<this.entities.Count;i++) 
			{
				xw.WriteElementString("item", this.entities[i].ToString());
			}
			xw.WriteEndElement();
			xw.WriteEndElement();
			xw.WriteEndDocument();
			xw.Flush();
			xw.Close();
		}

		
		public dbRepConfigBean() {}
	}
}
